import request from "./request"
import storage from "./storage"

// function formatMoney(num: number | string) {
//   // 格式化数字，每三位加逗号
//   return num.toString().replace(/(?<!\.\d*)(\d)(?=(\d{3})+(?!\d))/g, ",")
// }

function formatMoney(num: number | string) {
  const money = parseFloat(num.toString())
  return money.toLocaleString("zh-CN", { style: "currency", currency: "CNY" })
}
/**
 * 封装一个格式化日期的函数，可以指定具体格式，例如：yyyy-MM-dd HH:mm:ss
 */
function formatDate(
  date: Date = new Date(),
  format: string = "yyyy-MM-dd HH:mm:ss"
): string {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, "0")
  const day = String(date.getDate()).padStart(2, "0")
  const hours = String(date.getHours()).padStart(2, "0")
  const minutes = String(date.getMinutes()).padStart(2, "0")
  const seconds = String(date.getSeconds()).padStart(2, "0")

  return format
    .replace("yyyy", year.toString())
    .replace("MM", month)
    .replace("dd", day)
    .replace("HH", hours)
    .replace("mm", minutes)
    .replace("ss", seconds)
}

export { request, storage, formatMoney, formatDate }
